package com.leaf.guidesdemos;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ImageSpan;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.SearchView;
import android.widget.ShareActionProvider;

public class TestActivity extends Activity {

	private Uri uri;
	private EditText mEditText;
	private MenuItem menuItem;
	private ShareActionProvider mShareActionProvider;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// setContentView(R.layout.test);

		ActionBar actionBar = getActionBar();
		// getActionBar().setDisplayShowHomeEnabled(false);
//		actionBar.setDisplayShowTitleEnabled(false);
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

		Tab tab = actionBar
				.newTab()
				.setText("Artist!")
				.setTabListener(
						new TabListener<MainFragment>(this, "artist",
								MainFragment.class));
		actionBar.addTab(tab);
		tab = actionBar
				.newTab()
				.setText("Album!")
				.setTabListener(
						new TabListener<MainFragment>(this, "album",
								MainFragment.class));
		actionBar.addTab(tab);

	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		mEditText = (EditText) findViewById(R.id.edit);
	}

	public void pickPic(View view) {
		Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
		intent.setType("image/*");
		startActivityForResult(intent, 1);
	}

	public void search(View view) {

		startActivity(new Intent(this, MainActivity.class));

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (RESULT_OK == resultCode) {
			uri = data.getData();

			SpannableString ss = new SpannableString("[img]");
			Drawable d = getResources().getDrawable(
					android.R.drawable.ic_menu_gallery);
			d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
			ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BOTTOM);
			ss.setSpan(span, 0, 5, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
			mEditText.setText(ss);

		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		if (KeyEvent.KEYCODE_SEARCH == keyCode) {
			menuItem.expandActionView();
		}

		return super.onKeyUp(keyCode, event);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.activity_main, menu);

		menuItem = menu.findItem(R.id.menu_search);

		// menuItem.setOnActionExpandListener(new OnActionExpandListener() {
		//
		// @Override
		// public boolean onMenuItemActionExpand(MenuItem item) {
		//
		// getActionBar().setDisplayHomeAsUpEnabled(true);
		//
		// return true;
		// }
		//
		// @Override
		// public boolean onMenuItemActionCollapse(MenuItem item) {
		// getActionBar().setDisplayHomeAsUpEnabled(false);
		// return true;
		// }
		// });

		SearchView searchView = (SearchView) menuItem.getActionView();
		searchView.setQueryHint("leaf");

		mShareActionProvider = (ShareActionProvider) menu.findItem(
				R.id.menu_share).getActionProvider();
		mShareActionProvider
				.setShareHistoryFileName(ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME);

		mShareActionProvider.setShareIntent(createShareIntent());
		return true;
	}

	private Intent createShareIntent() {

		Intent intent = new Intent(Intent.ACTION_SEND);
		intent.setType("text/plain");
		intent.putExtra(Intent.EXTRA_TEXT, "i love u.");

		return intent;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		// case android.R.id.home:
		//
		// Intent intent=new Intent(this,TestActivity.class);
		// intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		// startActivity(intent);
		// break;

		case R.id.menu_share:

			return true;

		default:
			break;
		}

		return super.onOptionsItemSelected(item);
	}

	private static class TabListener<T extends Fragment> implements
			ActionBar.TabListener {

		private Fragment mFragment;
		private final Activity mActivity;
		private final String mTag;
		private final Class<T> mClass;

		public TabListener(Activity activity, String tag, Class<T> clz) {
			mActivity = activity;
			mTag = tag;
			mClass = clz;
		}

		@Override
		public void onTabSelected(Tab tab, FragmentTransaction ft) {

			if (mFragment == null) {
				mFragment = Fragment.instantiate(mActivity, mClass.getName());
				ft.add(android.R.id.content, mFragment, mTag);
			} else {
				ft.attach(mFragment);
			}

		}

		@Override
		public void onTabUnselected(Tab tab, FragmentTransaction ft) {

			if (mFragment != null) {
				ft.detach(mFragment);
			}

		}

		@Override
		public void onTabReselected(Tab tab, FragmentTransaction ft) {
			// TODO Auto-generated method stub

		}

	}

}
